//
//  LYNewFeatureViewController.m
//  lottery
//
//  Created by 赵国腾的MacBook Pro on 16/6/25.
//  Copyright © 2016年 赵国腾. All rights reserved.
//

#import "LYNewFeatureViewController.h"
#import "LYNewFeatureCell.h"

static NSString * const reuseIdentifier = @"cell";

@interface LYNewFeatureViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, assign) CGFloat lastOffsetX;

@property (nonatomic, strong) UIImageView *guideView;
@property (nonatomic, strong) UIImageView *guideLineView;
@property (nonatomic, strong) UIImageView *largerTextView;
@property (nonatomic, strong) UIImageView *smallTextView;

@end

@implementation LYNewFeatureViewController

- (instancetype)init {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0.0f;
    return [super initWithCollectionViewLayout:flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.collectionView.backgroundColor = [UIColor grayColor];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self.collectionView registerClass:[LYNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self setupAllChildViews];
}

// 添加collectionView子控件
- (void)setupAllChildViews {
    
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    guide.gt_midX = self.view.gt_midX;
    [self.collectionView addSubview:guide];
    self.guideView = guide;
    
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.gt_minX -= 150;
    [self.collectionView addSubview:guideLine];
    self.guideLineView = guideLine;

    UIImageView *largerText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largerText.gt_midX = self.view.gt_midX;
    largerText.gt_midY = self.view.gt_height * 0.7;
    [self.collectionView addSubview:largerText];
    self.largerTextView = largerText;

    UIImageView *smallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    smallText.gt_midX = self.view.gt_midX;
    smallText.gt_midY = self.view.gt_height * 0.8;
    [self.collectionView addSubview:smallText];
    self.smallTextView = smallText;
    
}

#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LYNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"guide%zdBackground", indexPath.row + 1];
    
    cell.image = [UIImage imageNamed:imageName];
    
    [cell setupIndexPath:indexPath count:4];
    
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.bounds.size;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat currOffsetX = scrollView.contentOffset.x;
    
    CGFloat delta = currOffsetX - _lastOffsetX;
    
    
    self.guideView.gt_minX += 2 * delta;
    self.largerTextView.gt_minX += 2 * delta;
    self.smallTextView.gt_minX += 2 * delta;

    [UIView animateWithDuration:0.25 animations:^{
        
        self.guideView.gt_minX -= delta;
        self.largerTextView.gt_minX -= delta;
        self.smallTextView.gt_minX -= delta;
        
    }];
    
    NSInteger page = currOffsetX / self.view.gt_width + 1;
    
    self.guideView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%zd", page]];
    self.largerTextView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideLargeText%zd", page]];
    self.smallTextView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideSmallText%zd", page]];
    
    self.lastOffsetX = currOffsetX;
    NSLog(@"%s", __func__);
}

@end
